package hmis.pos.Controller;

import hmis.pos.Common.pojo.ApiResponse;
import hmis.pos.Common.vo.Order.OrderAndOrderDetail;
import hmis.pos.Common.vo.Order.OrderAndTypeAndNumber;
import hmis.pos.Entity.Order;
import hmis.pos.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("order")
@CrossOrigin(origins={"http://localhost:3000"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("save")
    @ResponseBody
    public Order save(@RequestBody OrderAndTypeAndNumber orderAndTypeAndNumber){
        return orderService.save(orderAndTypeAndNumber);
    }

    @RequestMapping("savePayMessage")
    @ResponseBody
    public Order save(@RequestBody Order order){
        return orderService.savePayMessage(order);
    }

    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Order order){
        return orderService.delete(order);
    }

    @RequestMapping("update")
    @ResponseBody
    public Order update(@RequestBody Order order){
        return orderService.update(order);
    }

    @RequestMapping("findByStatus")
    @ResponseBody
    public List<OrderAndOrderDetail> findByStatus(@RequestParam String status){
        return orderService.findByStatus(status);
    }

    @RequestMapping("findById")
    @ResponseBody
    public Order findById(@RequestParam String id){
        return orderService.findById(id);
    }

    @RequestMapping("findAll")
    @ResponseBody
    public List<Order> findAll(){
        return orderService.findAll();
    }

    @RequestMapping("findAllOrder")
    @ResponseBody
    public List<OrderAndOrderDetail> findAllOrder(){
        return orderService.findAllOrder();
    }

    @RequestMapping("findByYear")
    @ResponseBody
    public List<OrderAndOrderDetail> findByYear(){
        return orderService.findByYear();
    }

    @RequestMapping("findByMonth")
    @ResponseBody
    public List<OrderAndOrderDetail> findByMonth(){
        return orderService.findByMonth();
    }

    @RequestMapping("findByWeek")
    @ResponseBody
    public List<OrderAndOrderDetail> findByWeek(){
        return orderService.findByWeek();
    }

    @RequestMapping("findByToday")
    @ResponseBody
    public List<OrderAndOrderDetail> findByToday(@RequestParam String today) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date utilDate = sdf.parse(today);
        Date date = new java.sql.Date(utilDate.getTime());
        return orderService.findByToday(date);
    }

    @RequestMapping("findByToday2")
    @ResponseBody
    public List<OrderAndOrderDetail> findByToday2(){
        return orderService.findByToday2();
    }

    @RequestMapping("renew")//修改订单信息
    @ResponseBody
    public ApiResponse<OrderAndOrderDetail> renew(@RequestBody Order order){
        return orderService.renew(order);
    }

    @RequestMapping("cancel")//取消订单
    @ResponseBody
    public ApiResponse<Order> cancel(@RequestBody Order order){
        return orderService.cancel(order);
    }
}
